package com.sujeet.learning.threads;

import java.util.concurrent.TimeUnit;

public class EvenOdd {

	public synchronized int incrPrint(int x) {
		x = x + 2;
		return x;
	}

	static Boolean flag = true;

	public static void main(String[] args) {
		final EvenOdd eo = new EvenOdd();
		Thread et = new Thread(new Runnable() {
			public void run() {
				try {
					int e = 0;
					while (true) {
						if (eo.flag) {
							e = eo.incrPrint(e);
							System.out.println("even:" + e);
							eo.flag = false;
							eo.notify();
 							if (e > 30) {
								break;
							}
							else {
								eo.wait();
							}
						}

					}
				} catch (InterruptedException e1) {
					e1.printStackTrace();
				}
			}
		});
		Thread ot = new Thread(new Runnable() {
			public void run() {
				try {
					int o = 1;
					while (true) {
						if (eo.flag) {
							o = eo.incrPrint(o);
							System.out.println("odd:" + o);
							eo.flag = false;
							eo.notify();
 							if (o > 30) {
								break;
							}
							else {
								eo.wait();
							}
						}

					}
				} catch (InterruptedException e1) {
					e1.printStackTrace();
				}
			}
		});
		ot.start();
		et.start();
	}

}
